package com.cloudfast.platform.dict.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cloudfast.platform.dict.entity.DictType;
import com.cloudfast.platform.dict.type.DictTypeDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cloudfast.common.page.Page;
import com.cloudfast.platform.dict.dao.DictDao;
import com.cloudfast.platform.dict.entity.Dict;

/**
 * @author liuyw
 * @描述:字典
 * @date 2020年4月30日
 */
@Service
@Transactional(readOnly = true, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
public class DictService {

    @Autowired
    private DictDao dictDao;

    @Autowired
    private DictTypeDao dictTypeDao;

    @Transactional(readOnly = false)
    public void save(Dict entity) {
        dictDao.saveOrUpdate(entity);
    }

    /**
     * 保存字典类型
     *
     * @param entity
     */
    @Transactional(readOnly = false)
    public void saveDictType(DictType entity) {
        dictTypeDao.saveOrUpdate(entity);
    }
    @Transactional(readOnly = false)
    public void deleteDictType(Long id) {
        dictTypeDao.deleteById(id);
    }
    /**
     * 查询字典类型
     *
     * @param entity
     */
    public List<DictType> findDictTypeList(DictType entity) {
        return dictTypeDao.findList("from DictType where delFlag='0'");
    }

    @Transactional(readOnly = false)
    public void delete(Long id) {
        dictDao.deleteById(id);
    }

    /**
     * @param id
     * @return
     * @描述:根据的查询
     * @author liuyw
     * @date 2020年4月12日
     */
    public Dict findOne(Long id) {
        return dictDao.findOne(id);
    }

    /**
     * 根据类型查询字典
     *
     * @param dictType
     * @return
     */
    public List<Dict> findListByDictType(String dictType) {
        Map<String, Object> params = new HashMap<>(1);
        params.put("dictType", dictType);
        StringBuilder hql = new StringBuilder(" from Dict where dictType=:dictType and delFlag='0'");
        return this.dictDao.findList(hql.toString(), params);
    }

    /**
     * 封装字典 用于table渲染
     *
     * @return
     */
    public Map<String, Object> findKeyValue() {
        Map<String, Object> ApiResponse = new HashMap<>();
        StringBuilder hql = new StringBuilder("from Dict where delFlag='0' group by dictType");
        List<Dict> children = dictDao.findList(hql.toString());
        for (Dict d : children) {
            List<Dict> values = findListByDictType(d.getDictType());
            Map<String, String> cValue = new HashMap<>();
            for (Dict c : values) {
                cValue.put(c.getValue(), c.getLabel());
            }
            ApiResponse.put(d.getDictType(), cValue);
        }
        return ApiResponse;
    }

    /**
     * 封装特定的字典信息(用于条件查询;及select)
     *
     * @return
     */
    public Map<String, Object> findAllByTypeKey() {
        Map<String, Object> ApiResponse = new HashMap<>();
        StringBuilder hql = new StringBuilder("from Dict where delFlag='0' group by dictType");
        List<Dict> children = dictDao.findList(hql.toString());
        for (Dict d : children) {
            List<Dict> values = findListByDictType(d.getDictType());
            ApiResponse.put(d.getDictType(), values);
        }
        return ApiResponse;
    }

    /**
     * 根据类型和值查询字典
     *
     * @param dictType
     * @return
     */
    public Dict findByDictTypeAndValue(String dictType, String value) {
        return this.dictDao.findByDictTypeAndValue(dictType, value);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param entity
     * @return
     */
    public Page<Dict> findPage(Page<Dict> page, Dict entity) {
        Map<String, Object> params = new HashMap<>(3);
        StringBuilder hql = new StringBuilder("from Dict where 1=1 ");
        if (StringUtils.isNotEmpty(entity.getDictType())) {
            hql.append(" and dictType=:dictType ");
            params.put("dictType", entity.getDictType());
        }
        if (StringUtils.isNotEmpty(entity.getLabel())) {
            hql.append(" and label=:label ");
            params.put("label", entity.getLabel());

        }
        if (StringUtils.isNotEmpty(entity.getValue())) {
            hql.append(" and value=:value ");
            params.put("value", entity.getValue());
        }
        hql.append(" and delFlag='0' ");

        return this.dictDao.findPage(page, hql.toString(), params);
    }
}
